package com.scau.ums.controller;

import com.scau.ums.mapper.StudentMapper;
import com.scau.ums.mapper.TeacherMapper;
import com.scau.ums.mapper.UserDIYMapper;
import com.scau.ums.model.Maintainer;
import com.scau.ums.model.Student;
import com.scau.ums.model.Teacher;
import com.scau.ums.service.ListUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xfy
 * @version 1.0
 * @date 2019/8/1 16:09
 */

/**
 * 显示用户列表的控制类
 */
@Controller
public class ListUserController {
    @Autowired
    UserDIYMapper userDIYMapper;
    @Autowired
    ListUserService service;

    @RequestMapping("/listUser")
    public String listUser(HttpServletRequest request){

        return "listUser";
    }

    /**
     * 分页显示学生列表
     */
    @RequestMapping("/getStudentList")
    @ResponseBody
    public Map<String, Object> listStudent(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        String searchKeyword = request.getParameter("search");
        if(searchKeyword == null)
            searchKeyword = "";
        int totalRecord = userDIYMapper.getStudentNumber(searchKeyword);

        System.out.println("搜索内容:" + searchKeyword);
        System.out.println("总共页数：" + totalRecord);
        System.out.println("当前页：" + curPage);

        map.put("totalRecord",totalRecord);
        map.put("keyWord",searchKeyword);
        int sumPage = service.getStudentPageSum(searchKeyword);
        map.put("sumPage", new Integer(sumPage));
        List<Student> studentList = service.getStudentCurPageRecords(curPage, searchKeyword);
        map.put("studentList",studentList);

        return map;
    }

    /**
     * 分页显示教师列表
     */
    @RequestMapping("/getTeacherList")
    @ResponseBody
    public Map<String, Object> listTeacher(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        String searchKeyword = request.getParameter("search");
        if(searchKeyword == null)
            searchKeyword = "";
        int totalRecord = userDIYMapper.getTeacherNumber(searchKeyword);

        System.out.println("搜索内容:" + searchKeyword);
        System.out.println("总共页数：" + totalRecord);
        System.out.println("当前页：" + curPage);

        map.put("totalRecord",totalRecord);
        map.put("keyWord",searchKeyword);
        int sumPage = service.getTeacherPageSum(searchKeyword);
        map.put("sumPage", new Integer(sumPage));
        List<Teacher> teacherList = service.getTeacherCurPageRecords(curPage, searchKeyword);
        map.put("teacherList",teacherList);

        return map;
    }

    /**
     * 分页显示维修工列表
     */
    @RequestMapping("/getMaintainerList")
    @ResponseBody
    public Map<String, Object> listMaintainer(HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        int curPage = Integer.parseInt(request.getParameter("curPage"));
        String searchKeyword = request.getParameter("search");
        if(searchKeyword == null){
            searchKeyword = "";
        }
        int totalRecord = userDIYMapper.getMaintainerNumber(searchKeyword);

        System.out.println("搜索内容:" + searchKeyword);
        System.out.println("总共页数：" + totalRecord);
        System.out.println("当前页：" + curPage);

        map.put("totalRecord",totalRecord);
        map.put("keyWord",searchKeyword);
        int sumPage = service.getMaintainerPageSum(searchKeyword);
        map.put("sumPage", new Integer(sumPage));
        List<Map<String,Object>> maintainerList = service.getMaintainerCurPageRecords(curPage, searchKeyword);
        map.put("maintainerList",maintainerList);

        return map;
    }
}
